function rangeAddQueries(n, queries) {
  // 初始化 (n+2) x (n+2) 的差分矩阵，额外空间避免边界判断
  const diff = Array.from({ length: n + 2 }, () => new Array(n + 2).fill(0));

  for (const [row1, col1, row2, col2] of queries) {
    // 差分矩阵更新：四个关键位置
    diff[row1][col1] += 1;
    diff[row1][col2 + 1] -= 1;
    diff[row2 + 1][col1] -= 1;
    diff[row2 + 1][col2 + 1] += 1;
  }

  // 第一步：计算每行的前缀和
  for (let i = 0; i < n; i++) {
    let prefix = 0;
    for (let j = 0; j < n; j++) {
      prefix += diff[i][j];
      diff[i][j] = prefix;
    }
  }

  // 第二步：计算每列的前缀和，得到最终矩阵
  for (let j = 0; j < n; j++) {
    let prefix = 0;
    for (let i = 0; i < n; i++) {
      prefix += diff[i][j];
      diff[i][j] = prefix;
    }
  }

  // 截取 n x n 的结果矩阵（去掉额外的边界空间）
  const result = [];
  for (let i = 0; i < n; i++) {
    result.push(diff[i].slice(0, n));
  }

  return result;
}
